package com.zzh.myapplication.activity;

import android.app.ActionBar;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import net.icarplus.car.R;
import net.icarplus.car.adapter.RoadTripAdapter;
import net.icarplus.car.bean.RoapTripBean;
import net.icarplus.car.constant.UrlValues;
import net.icarplus.car.manager.BaseActivity;
import net.icarplus.car.service.PublicRequest;
import net.icarplus.car.tools.SPUtils;
import net.icarplus.car.tools.ToolBarHelper;
import net.icarplus.car.views.WaterDropListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/3/16.
 * *类描述：
 * 创建人：zhangzhonghua
 * 创建时间：16/3/16 10:37
 * 修改人：
 * 修改时间：16/3/16 10:37
 * 修改备注：
 */
public class RoadtripActivity extends BaseActivity implements WaterDropListView.IWaterDropListViewListener,AdapterView.OnItemClickListener {
    private WaterDropListView waterDropListView;
    public static final int CASE = 1;
    public RoadTripAdapter adapter;
    public List<RoapTripBean> list;
    public LinearLayout type;
    public LinearLayout sort;
    public LinearLayout distance;
    public PopupWindow popuwindow;
    public View view;
    private Toolbar toolbar;

    private ImageView ivTripType;
    private ImageView ivTripDistance;
    private ImageView ivTripSort;
    private boolean isUp = false;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    waterDropListView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    break;
                case CASE:
                    waterDropListView.stopRefresh();
                    break;
                case 3000:
                    System.out.print("3000" + msg.obj.toString());
                    break;
                case 1000:
                    if (msg.obj.toString() != null) {
                        try {
                            JSONObject object = new JSONObject(msg.obj.toString());
                            JSONArray array = object.getJSONArray("queryTravelList");
                            RoapTripBean tripBean = null;
                            for (int i = 0; i < array.length(); i++) {
                                JSONObject o = array.optJSONObject(i);
                                tripBean = new RoapTripBean();
                                tripBean.setAddrass(o.optString("travelTitle"));
                                tripBean.setCarUrl(o.optString("coverImg"));
                                tripBean.setPrice(o.optString("favorablePrice"));
                                tripBean.setPrint(o.optString("saleNum"));
                                tripBean.setTime(o.optString("minDay"));
                            }
                            Message message = new Message();
                            message.what = 0;
                            message.obj = list.add(tripBean);
                            handler.sendMessage(message);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    break;

            }
        }
    };

    @Override
    public void setContent() {
        setContentView(R.layout.activity_roadtrip);
    }

    // 找到Id
    @Override
    public void initComponent() {
        waterDropListView = (WaterDropListView) findViewById(R.id.wdlistview);
        distance = (LinearLayout) findViewById(R.id.distance);
        type = (LinearLayout) findViewById(R.id.type);
        sort = (LinearLayout) findViewById(R.id.sort);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        ivTripType = (ImageView) findViewById(R.id.iv_trip_type);
        ivTripDistance = (ImageView) findViewById(R.id.iv_trip_distance);
        ivTripSort = (ImageView) findViewById(R.id.iv_trip_sort);
    }

    @Override
    public void setListener() {
        waterDropListView.setWaterDropListViewListener(this);
        waterDropListView.setPullLoadEnable(true);
        waterDropListView.setOnItemClickListener(this);
        distance.setOnClickListener(this);
        type.setOnClickListener(this);
        sort.setOnClickListener(this);
    }

    @Override
    public void onAfterInit() {
        GetTravelParams();
        ToolBarHelper.setNormalTitleAndBack(this, toolbar, getString(R.string.car_travel));
        list = new ArrayList<RoapTripBean>();
        adapter = new RoadTripAdapter(list,RoadtripActivity.this);
    }

    public void GetTravelParams() {
        JSONObject object = new JSONObject();
        try {
            object.put("mobile", SPUtils.getString(this, "LoginInfo", ""));
            object.put("positionLon", "103.111");
            object.put("positionLat", "19.001");
            object.put("themeType", "");
            object.put("distance", "");
            object.put("orderBy", "01");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        PublicRequest.customMainRequest(this, UrlValues.ROAP_TRIP_URL, handler, object);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.distance:
                ShowPopuWindow(distance, R.layout.roadtrip_tepy);
                ivTripDistance.setImageResource(R.mipmap.up);
                break;
            case R.id.sort:
                ShowPopuWindow(sort, R.layout.roadtrip_tepy);
                ivTripSort.setImageResource(R.mipmap.up);
                break;
            case R.id.type:
                ShowPopuWindow(type, R.layout.roadtrip_tepy);
                ivTripType.setImageResource(R.mipmap.up);
//                setArrowDirection(ivTripType, R.mipmap.up);
                break;
        }
    }

    public void ShowPopuWindow(View v, int m) {
        view = View.inflate(RoadtripActivity.this, m, null);
        popuwindow = new PopupWindow();
        popuwindow.setWidth(ActionBar.LayoutParams.MATCH_PARENT);
        popuwindow.setHeight(ActionBar.LayoutParams.MATCH_PARENT);
        popuwindow.setContentView(view);
        popuwindow.setFocusable(true);//设置焦点
        popuwindow.setOutsideTouchable(true);
        //popuwindow.setBackgroundDrawable(getResources().getDrawable(R.mipmap.icon_id_card));
        popuwindow.showAsDropDown(v);
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                popuwindow.dismiss();
                ivTripType.setImageResource(R.mipmap.down);
                ivTripDistance.setImageResource(R.mipmap.down);
                ivTripSort.setImageResource(R.mipmap.down);
                return true;
            }
        });
    }

    @Override
    public void onRefresh() {
        handler.sendEmptyMessage(1);
    }
    @Override
    public void onLoadMore() {
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}
